import cgi
import wsgiref.handlers
import random
import os
import logging

from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp


global LOGINSTATUS
LOGINSTATUS = "unknown"

def tpl_path(template_file_name):
    return os.path.join(os.path.dirname(__file__),
                        'templates', template_file_name)

def login_url(uri):
  # Construct Login/Logout URL.
  if users.get_current_user():
    url = users.create_logout_url(uri)
    url_linktext = 'Logout'
  else:
    url = users.create_login_url(uri)
    url_linktext = 'Login'
  return url

def login_text():
  # Construct Login/Logout Text.
  if users.get_current_user():
    LOGINSTATUS = "logged in"
    url_linktext = 'Logout'
  else:
    url_linktext = 'Login'
  return url_linktext

def raise_error(error_string):
    # Raise and Log Error
    logging.error(error_string)
    raise error_string
